/** 网络请求工具类的拓展类，便于后期网络层修改维护 **/

import HttpUtils from "./HttpUtils";
import { show } from "../constants/Toast";
import NavigationService from '../constants/NavigationService';
import { api_Base64ToUrl } from "./HttpServices";
import { remove, LoginToken } from "./MyStorage";

//http://a758_admin.mxnt.net/realapi/
//http://a758_admin.mxnt.net/
export const API_URL = "http://a806.mxnt.net/app/api.php?i=1&r=";

export const API_URL_1 = "http://jj1.vip/api/";



/**
 * GET
 * 从缓存中读取数据
 * @param url 请求url
 * @param params 请求参数
 * @param callback 是否有回调函数
 * @returns {value\promise}
 * 返回的值如果从缓存中取到数据就直接换行数据，或则返回promise对象
 */
const fetchData = (type) => (url, params, callback) => {
    // url = `${url}`;
    url = `${API_URL}${url}`;
    console.log('url:' + url + '   ' + 'token:' + global.token);
    const fetchFunc = () => {
        let promise =
            type === "get" ? HttpUtils.getRequest(url, params) : HttpUtils.postRequrst(url, params);
        if (callback && typeof callback === "object") {
            promise.then((response) => {
                if (response) {
                    if (response.status == 1) {
                        return callback.success(response);
                    } else if (response.status == 401) {
                        global.token = undefined;
                        global.userInfo = undefined;
                        global.companyInfo = undefined;
                        remove(LoginToken);
                        show('请进行登录');
                        NavigationService.navigate('Login');
                    } else {
                        console.warn(response.msg);
                        return callback.fail(response);
                    }
                }
            });
        }
        return promise;
    };
    fetchFunc();
};

const postQueryFetch = (url, params, callback) => {
    // url = `${url}`;
    url = `${API_URL}${url}`;
    console.log(`请求URL: ${url}`);
    const fetchFunc = () => {
        params['token'] = global.userInfo ? global.userInfo.token : '';
        params['openid'] = global.userInfo ? global.userInfo.member.openid : '';
        let promise = HttpUtils.postQueryRequrst(url, params);
        if (callback && typeof callback === "object") {
            promise.then((response) => {
                if (response) {
                    if (response.returnCode == '1') {
                        return callback.success(response);
                    } else {
                        console.warn(response.msg);
                        return callback.fail(response);
                    }
                }
            });
        }
        return promise;
    };
    fetchFunc();
};

/**
 * POST
 * 从缓存中读取数据
 * @param url 请求url
 * @param params 请求参数
 * @param callback 是否有回调函数
 * @returns {value\promise}
 * 上传base64文件的方法
 */
const fetchBase64Data = (type) => (params, callback) => {
    // url = `${url}`;
    url = `${API_URL_1}${api_Base64ToUrl}`;
    console.log('url:' + url + '   ' + 'token:' + global.token);
    const fetchFunc = () => {
        let promise =
            type === "get" ? HttpUtils.getRequest(url, params) : HttpUtils.postRequrst(url, params);
        if (callback && typeof callback === "object") {
            promise.then((response) => {
                if (response) {
                    if (response.code == 200) {
                        return callback.success(response);
                    } else {
                        console.warn(response.msg);
                        return callback.fail(response);
                    }
                }
            });
        }
        return promise;
    };
    fetchFunc();
};
/**
 * GET 请求
 * @param url
 * @param params
 * @param source
 * @param callback
 * @returns {{promise: Promise}}
 */
const getFetch = fetchData("get");

/**
 * POST 请求
 * @param url
 * @param params
 * @param callback
 * @returns {{promise: Promise}}
 */
const postFetch = fetchData("post");

const postBase64Fetch = fetchBase64Data("post");

export { getFetch, postFetch, postQueryFetch, postBase64Fetch };